// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package ptn

import (
	"github.com/palletone/go-palletone/common"
	//"github.com/palletone/go-palletone/common/bitutil"
	"github.com/palletone/go-palletone/common/bloombits"
	"github.com/palletone/go-palletone/common/log"
	"github.com/palletone/go-palletone/dag/modules"
)

var (
	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
)

// startBloomHandlers starts a batch of goroutines to accept bloom bit database
// retrievals from possibly a range of filters and serving the data to satisfy.
//func (eth *PalletOne) startBloomHandlers() {
//for i := 0; i < bloomServiceThreads; i++ {
//	go func() {
//		for {
//			select {
//			case <-eth.shutdownChan:
//				return
//
//			case request := <-eth.bloomRequests:
//				task := <-request
//				task.Bitsets = make([][]byte, len(task.Sections))
//				//for i,section:= range task.Sections {
//				//section = section
//				//task.Error = errors.New("404 Not Found")
//				//head := core.GetCanonicalHash(eth.chainDb, (section+1)*BloomBitsBlocks-1)
//				//if compVector, err := core.GetBloomBits(eth.chainDb, task.Bit, section, head); err == nil {
//				//	if blob, err := bitutil.DecompressBytes(compVector, int(BloomBitsBlocks)/8); err == nil {
//				//		task.Bitsets[i] = blob
//				//	} else {
//				//		task.Error = err
//				//	}
//				//} else {
//				//	task.Error = err
//				//}
//				//}
//				request <- task
//			}
//		}
//	}()
//}
//}

const (
// bloomConfirms is the number of confirmation blocks before a bloom section is
// considered probably final and its rotated bits are calculated.
//bloomConfirms = 256

// bloomThrottling is the time to wait between processing two consecutive index
// sections. It's useful during chain upgrades to prevent disk overload.
//bloomThrottling = 100 * time.Millisecond
)

// BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
// for the Ethereum header bloom filters, permitting blazing fast filtering.
type BloomIndexer struct {
	size uint64 // section size to generate bloombits for

	//db  ptndb.Database       // database instance to write index data and metadata into
	gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index

	section uint64      // Section is the section number being processed currently
	head    common.Hash // Head is the hash of the last header processed
}

// NewBloomIndexer returns a chain indexer that generates bloom bits data for the
// canonical chain for fast logs filtering.
//func NewBloomIndexer(db ptndb.Database, size uint64) *indexer.ChainIndexer {
//	backend := &BloomIndexer{
//		db:   db,
//		size: size,
//	}
//	table := ptndb.NewTable(db, string(BloomBitsIndexPrefix))
//
//	return indexer.NewChainIndexer(db, table, backend, size, bloomConfirms, bloomThrottling, "bloombits")
//}

// Reset implements core.ChainIndexerBackend, starting a new bloombits index
// section.
func (b *BloomIndexer) Reset(section uint64, lastSectionHead common.Hash) error {
	gen, err := bloombits.NewGenerator(uint(b.size))
	b.gen, b.section, b.head = gen, section, common.Hash{}
	return err
}

// Process implements core.ChainIndexerBackend, adding a new header's bloom into
// the index.
func (b *BloomIndexer) Process(header *modules.Header) {
	log.Debug("========BloomIndexer Process========")
	//TODO must be recover
	//b.gen.AddBloom(uint(header.Number.Index-b.section*b.size), header.Bloom)
	//b.head = header.Hash()
}

// Commit implements core.ChainIndexerBackend, finalizing the bloom section and
// writing it out into the database.
func (b *BloomIndexer) Commit() error {
	return nil
	//	batch := b.db.NewBatch()
	//
	//	for i := 0; i < types.BloomBitLength; i++ {
	//		bits, err := b.gen.Bitset(uint(i))
	//		if err != nil {
	//			return err
	//		}
	//		bits = bits
	//		//TODO must be recover
	//		//core.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
	//	}
	//	return batch.Write()
}
